<?php
class Po extends CI_Controller{

    private $limit = 10;
	
	function  __construct() {
    parent::__construct();
    $this->load->model('Po_model');
    $this->load->model('Cabang_model');
    $this->load->model('Barang_model');
    $this->load->model('User_model');
	$this->load->model('Penjualan_model');
	$this->load->model('Laporan_model');
    $this->load->helper(array('form', 'url'));
    $this->load->library(array('form_validation', 'pagination'));
    }

   function index(){

       if ($this->session->userdata('is_logged_in') == TRUE) {
                $this->load->view('admin_views/po/index');

             }
        else {
            redirect('login');
        }
    }
    function home(){

        $this->load->library('pagination'); 
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('po/home/');
		$config['total_rows'] = $this->Po_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		$data['jumlah_data']=$this->Po_model->countData();

		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

		$data['cabang'] = $nm_cabang[0]->strnama_cabang;
        $data['id_cabang'] = $nm_cabang[0]->intid_cabang;

		$data['po']	= $this->Po_model->get_list_data($this->limit,$offset, $nm_cabang[0]->strnama_cabang);
        $data['user'] = $this->session->userdata('username');

	    $this->load->view('admin_views/po/po', $data);
    }


	function pusat(){
		$this->load->library('pagination'); 
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('po/pusat/');
		$config['total_rows'] = $this->Po_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		$data['jumlah_data']=$this->Po_model->countData();
		
		if($_POST){
			$data['po']	= $this->Po_model->get_list_data_filter($this->limit,$offset, $_POST);	
		}else{
			$data['po']	= $this->Po_model->get_list_data_pusat($this->limit,$offset);
		}
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;


            $data['user'] = $this->session->userdata('username');
       $this->load->view('admin_views/po/pusat', $data);
	}
	
	function sjpusat(){
	
		$this->load->view('admin_views/po/sjpusat');
	}
	
	 function sjdata(){
	
		$week = $this->Po_model->selectWeek();
		foreach ($week as $g)
		{
			$data['id'][]	 	= $g->id;
			$data['intid_week'][] 	= $g->intid_week;
		}
		
		if($_POST) {
                        
            $id_cabang = $this->input->post('id_cabang');
			$week = $this->input->post('week');
			$data['default'] = $this->Po_model->get_data_sj($id_cabang, $week);
            $this->load->view('admin_views/po/sjdata', $data);
        }else {
            
            $this->load->view('admin_views/po/sjdata', $data);
        }
		
	}
	
	function sjAdd(){
		
		if ($_POST)
		{
		 	$data['default'] = $this->Po_model->selectSpkb($this->input->post('id_cabang'));
		 	$this->load->view('admin_views/po/sjAdd', $data);
		}else {
		    $this->load->view('admin_views/po/sjAdd');
 		}       
	}
	
	function buat_sj(){
		
		$detail =  $this->input->post('pilih');
		for($i=0; $i<count($detail); $i++)
			{
				//echo $id=($detail[$i].","); 	
				$data['default']    = $this->Po_model->get_Barang($detail[$i]);
			}
		
		$this->load->view('admin_views/po/sjCreate', $data);
	}
	
	function poAdd(){
		if (!$_POST)
		{
			
			$max = $this->Po_model->get_MaxPo();
			
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$data['week'] = $this->Penjualan_model->selectWeek();
			$data['maxId'] = $max->intid_po;

            $data['user'] = $this->session->userdata('username');
			if(empty($max->intid_po)){
				$data['maxId'] =1;
			} else {
				$data['maxId'] = $max->intid_po + 1;
			}

			$this->load->view('admin_views/po/add', $data);
		 }else{

            $ins = $this->Po_model->insert($_POST);

            $id = $ins;
            $data = $this->input->post('barang');
            for($i=1;$i<=sizeof($data);$i++){
            if(!empty($data[$i]['intid_id'])){
            $detail = array(
                            'intid_po' 		=> $id,
                            'intid_barang'	=> $data[$i]['intid_id'],
                            'reg_pcs'	=> $data[$i]['pcs_reg'],
							'reg_set'	=> $data[$i]['set_reg'],
							'free_pcs'	=> $data[$i]['pcs_free'],
							'free_set'	=> $data[$i]['set_free'],
							'ket'		=> $data[$i]['ket']
                            );

             $this->Po_model->add($detail);
            }
            }

			redirect('po/cetak_po');
		}

	}

    function cetak_po(){
        $max = $this->Po_model->get_MaxPo();
        $id = $max->intid_po;
        $ada = $this->Po_model->get_CetakPo($id);
        $data['default'] = $this->Po_model->get_CetakPo($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
        $this->load->view('admin_views/po/cetak_po', $data);
	}
	
	function cetak_po_view($id){
        $ada = $this->Po_model->get_CetakPo($id);
        $data['default'] = $this->Po_model->get_CetakPo($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
        $this->load->view('admin_views/po/cetak_po_pusat', $data);
	}
	
	function cetak_surat_keluar($id){
        $data['default'] = $this->Po_model->get_CetakPo($id);
		
		$data['week'] = $this->Penjualan_model->selectWeek();
		$data['surat_keluar'] = "surat_keluar";
		
		$cek = $this->Po_model->cek_spkb($data['default'][0]->intid_po);
		if(empty($cek->spbk)){
				$spkb =  $data['default'][0]->intid_po."/".$data['week'][0]->intid_week."/SPKB/".date('Y');
				$this->Po_model->update_spkb($spkb,$data['default'][0]->intid_po);
		}
        $this->load->view('admin_views/po/cetak_po', $data);
	}
	
	function sj() {

		$this->load->library('pagination'); 
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('po/sj/');
		$config['total_rows'] = $this->Po_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
        $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		$data['jumlah_data']=$this->Po_model->countData();
		
		$data['po']	= $this->Po_model->get_list_data_sj($this->limit,$offset, $cabang[0]->intid_cabang);
        $data['user'] = $this->session->userdata('username');
        $week = $this->Po_model->selectWeek();
		foreach ($week as $g)
		{
			$data['id'][]	 	= $g->id;
			$data['intw'][] 	= $g->intid_week;
		}
		$this->load->view('admin_views/po/sj', $data);

	}
	
	function sretur()
	{
		$this->load->view('admin_views/po/sretur');
	}
	
	function returAdd(){
		if (!$_POST)
		{

            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$data['week'] = $this->Penjualan_model->selectWeek();
			$week = $this->Po_model->selectWeek();
			foreach ($week as $g)
			{
				$data['id'][]	 	= $g->id;
				$data['intw'][] 	= $g->intid_week;
			}

            $data['user'] = $this->session->userdata('username');
			$max = $this->Po_model->get_MaxRetur();
			if(empty($max->intid_retur)){
				$data['maxId'] =1;
			} else {
				$data['maxId'] = $max->intid_retur + 1;
			}
			$this->load->view('admin_views/po/retur_add', $data);
		 }else{

            $ins = $this->Po_model->insert_surat_retur($_POST);

            $id = $ins;
            $data = $this->input->post('barang');
            for($i=1;$i<=sizeof($data);$i++){
            if(!empty($data[$i]['intid_id'])){
            $detail = array(
                            'intid_retur' 	=> $id,
                            'intid_barang'	=> $data[$i]['intid_id'],
                            'reg_pcs'		=> $data[$i]['pcs_reg'],
							'reg_set'		=> $data[$i]['set_reg'],
							'free_pcs'		=> $data[$i]['pcs_free'],
							'free_set'		=> $data[$i]['set_free'],
							'ket'			=> $data[$i]['ket']
                            );

             //var_dump($detail[$i]['intid_id']);
			 $this->Po_model->add_surat_retur($detail);
			 $this->Po_model->minusStok($data[$i]['intid_id'], $data[$i]['pcs_reg'], $data[$i]['set_reg'], $data[$i]['pcs_free'], $data[$i]['set_free'], $this->input->post('intid_cabang'));
			 
            }
            }

			redirect('po/cetak_surat_retur/'.$id);
		}

	}
	
	function surat_retur() {

		$this->load->library('pagination'); 
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('po/surat_retur/');
		$config['total_rows'] = $this->Po_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		$data['jumlah_data']=$this->Po_model->countDataRetur();
		
                
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$data['po']	= $this->Po_model->get_list_data_retur($this->limit,$offset, $nm_cabang[0]->intid_cabang);

            $data['user'] = $this->session->userdata('username');
            $week = $this->Po_model->selectWeek();
			foreach ($week as $g)
			{
				$data['id'][]	 	= $g->id;
				$data['intw'][] 	= $g->intid_week;
			}
		$this->load->view('admin_views/po/retur', $data);

	}
	
	function kartu_stok()
    {
	if (!$_POST)
	{	
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
        $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		$this->load->view('admin_views/po/kartu_stok', $data);
    } else {
		
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
        $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('po/view_kartustok/');
		$config['total_rows'] = $this->Po_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		
		$id_cabang = $this->input->post('id_cabang');
		$tgl = $this->input->post('datetgl');
		$id_barang = $this->input->post('id_barang');
		$data['po'] = $this->Po_model->getKartu_Stok($id_cabang, $tgl, $id_barang);
		$this->load->view('admin_views/po/kartu_stok', $data);
		
		}
	}
		
	function lap_stok_cab()
    {
		    
			$cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$week = $this->Po_model->selectWeek();
			foreach ($week as $g)
			{
				$data['id'][]	 	= $g->id;
				$data['intw'][] 	= $g->intid_week;
			}  
		if (!$_POST)
		{
            $data['user'] = $this->session->userdata('username');
            $data['po'] = "";
            $this->load->view('admin_views/po/lap_stok_cab', $data);
		 
		 }else{
					
			$intid_cabang = $this->input->post('intid_cabang');
			$intid_week = $this->input->post('intid_week');
			$nama = $this->input->post('nama');
			$data['id_cabang'] = $intid_cabang;
			$data['intid_week'] = $intid_week;
			$data['user'] = $this->session->userdata('username');
			$nm_cabang = $this->Cabang_model->select($intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['po'] = $this->Po_model->getLap_Stok_cab($intid_cabang, $intid_week);
						
            $this->load->view('admin_views/po/cetak_lap_stok_cab', $data);
		 }
       
        }
		
		function Lap_stock_op() {
		
			$cabang = $this->User_model->getCabang($this->session->userdata('username'));
			$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
	
			$data['cabang'] = $nm_cabang[0]->strnama_cabang;
			$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
	
	
			$data['user'] = $this->session->userdata('username');
		
			if ($_POST)
			{
				$this->load->library('pagination'); 
				$uri_segment = 3;
				$offset = $this->uri->segment($uri_segment);
				$config['base_url'] = site_url('po/lap_stok_cab/');
				$config['total_rows'] = $this->Po_model->countData();
				$config['per_page'] = $this->limit;
				$config['num_links'] = 2;
				$config['full_tag_open'] = '<div id="pagination">';
				$config['full_tag_close'] = '</div>';
		
				$this->pagination->initialize($config);
				$data['pagination'] = $this->pagination->create_links();
				$data['offset']=$offset;
				
				$intid_cabang = $this->input->post('intid_cabang');
				$tgl = $this->input->post('datetgl');
				$nama = $this->input->post('nama');
				$data['id_cabang'] = $intid_cabang;
				$data['user'] = $this->session->userdata('username');
				$data['po'] = $this->Po_model->getLap_Stok_Op($this->limit, $offset, $intid_cabang, $tgl);
			}
		$this->load->view('admin_views/po/stok_offname', $data);
	}
	
	function view_po($id) {
		$data['po']	= $this->Po_model->get_list_data_detail($id);
        $this->load->view('admin_views/po/view_detail_po', $data);
    }
	
	function cetak_sj(){
        
		$data['week'] = $this->Penjualan_model->selectWeek();
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
        $id_cabang = $nm_cabang[0]->intid_cabang;
		$tgl = date("Y-m-d");
		$max = $this->Po_model->get_MaxSj();
		if(empty($max->intid_sj)){
			$maxId =1;
		} else {
			$maxId = $max->intid_sj + 1;
		}
		$po = $this->input->post('spkb');
		$spkb = $this->Po_model->spkb($po);
		$no_spkb = $spkb[0]->no_spkb;
		$no_sj = $maxId."/".$data['week'][0]->intid_week."/SJ/".date('Y');
		$sj = array(
                            'no_sj' 		=> $no_sj,
                            'no_spkb'		=> $no_spkb,
                            'intid_cabang'	=> $id_cabang,
							'intid_week'	=> $data['week'][0]->intid_week,
							'datetgl'		=> $tgl
							
							);
		$ins = $this->Po_model->insert_surat_jalan($sj);
       	$this->Po_model->update_po($po);
		$detail =  $this->input->post('detail');
		$count_detail = count($detail);
		
			for($i=0; $i<$count_detail; $i++)
			{
				if(isset($detail[$i]['intid_barang'])){
					$_data['intid_sj'] 				= $ins;
					$_data['intid_barang'] 			= $detail[$i]['intid_barang'];
					$_data['reg_pcs'] 				= $detail[$i]['reg_pcs'];					
					$_data['reg_set'] 				= $detail[$i]['reg_set'];
					$_data['free_pcs'] 				= $detail[$i]['free_pcs'];
					$_data['free_set'] 				= $detail[$i]['free_set'];
					$_data['ket'] 					= $detail[$i]['ket'];
					
					$this->Po_model->add_surat_jalan($_data);
					$this->Po_model->addStok($detail[$i]['intid_barang'], $detail[$i]['reg_pcs'], $detail[$i]['reg_set'], $detail[$i]['free_pcs'], $detail[$i]['free_set'], $id_cabang);
				}
			}
		$data['default'] = $this->Po_model->get_cetak_surat_jalan($ins);
		$data['week'] = $this->Penjualan_model->selectWeek();
		$this->load->view('admin_views/po/cetak_surat_jalan', $data);
	}
	
	function view_sj($id) {
		$data['default'] = $this->Po_model->get_cetak_surat_jalan($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
		$this->load->view('admin_views/po/cetak_surat_jalan', $data);
    }
	
	function lookupBarang(){
	$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Po_model->selectBarang($keyword);
        if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
                                        'value' => $row->strnama,
                                        ''
                                     );
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data);
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
     function lookupBarangRetur(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Po_model->selectBarangRetur($keyword);
        if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
                                        'value' => $row->strnama,
                                        'value1' => $row->strnama_jsatuan,
                                        ''
                                     );
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data);
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	
	function lookupCabang(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Po_model->selectCabang($keyword);
        if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->intid_cabang,
                                        'value' => $row->strnama_cabang,
                                        ''
                                     );
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data);
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	
	function cetak_surat_retur($id){
		$data['default'] = $this->Po_model->get_cetak_surat_retur($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
        $this->load->view('admin_views/po/cetak_surat_retur', $data);
	}
	

	function sreturpusat(){
		$this->load->library('pagination'); 
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('po/sreturpusat/');
		$config['total_rows'] = $this->Po_model->countDataRetur();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		$data['jumlah_data']=$this->Po_model->countDataRetur();
		
		if($_POST){
			$data['po']	= $this->Po_model->get_list_data_filter_retur($this->limit,$offset, $_POST);	
		}else{
			$data['po']	= $this->Po_model->get_list_retur_data($this->limit,$offset);
		}
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;


            $data['user'] = $this->session->userdata('username');
        $this->load->view('admin_views/po/returpusat', $data);
	}
	
	function view_retur($id) {
		$data['po']	= $this->Po_model->get_list_data_retur_detail($id);
        $this->load->view('admin_views/po/view_detail_retur', $data);
    }
	
	function sttb_param() {
		
		$this->load->view('admin_views/po/sttb_param');
    }
	
	function sttb() {

		$this->load->library('pagination'); 
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('po/sttb/');
		$config['total_rows'] = $this->Po_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		$data['jumlah_data']=$this->Po_model->countDataSttb();
		$data['po']	= $this->Po_model->get_list_data_sttb($this->limit,$offset);
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;


            $data['user'] = $this->session->userdata('username');
            $week = $this->Po_model->selectWeek();
			foreach ($week as $g)
			{
				$data['id'][]	 	= $g->id;
				$data['intw'][] 	= $g->intid_week;
			}
		$this->load->view('admin_views/po/sttb', $data);

	}

		function sttbAdd(){
		
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
        $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		$srb = $this->Po_model->selectSrb();
			foreach ($srb as $g)
			{
				$data['intid_retur'][]	 	= $g->intid_retur;
				$data['no_srb'][] 			= $g->no_srb;
			}
			
           $this->load->view('admin_views/po/sttbAdd', $data);

	}
	function cetak_sttb(){
        
		$data['week'] = $this->Penjualan_model->selectWeek();
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
        $id_cabang = $nm_cabang[0]->intid_cabang;
		$tgl = date("Y-m-d");
		$max = $this->Po_model->get_MaxSttb();
		if(empty($max->intid_retur)){
			$maxId =1;
		} else {
			$maxId = $max->intid_retur + 1;
		}
		$po = $this->input->post('srb');
		$srb = $this->Po_model->srb($po);
		$no_srb = $srb[0]->no_srb;
		$no_sttb = $maxId."/".$data['week'][0]->intid_week."/STTB/".date('Y');
		$sttb = array(
                            'no_sttb' 		=> $no_sttb,
                            'no_srb'		=> $no_srb,
                            'intid_cabang'	=> $id_cabang,
							'intid_week'	=> $data['week'][0]->intid_week,
							'datetgl'		=> $tgl
							
							);
		$ins = $this->Po_model->insert_sttb($sttb);
       	$this->Po_model->update_retur($po);
		$detail =  $this->input->post('detail');
		$count_detail = count($detail);
		
			for($i=0; $i<$count_detail; $i++)
			{
				if(isset($detail[$i]['intid_barang'])){
					$_data['intid_sttb'] 				= $ins;
					$_data['intid_barang'] 			= $detail[$i]['intid_barang'];
					$_data['reg_pcs'] 				= $detail[$i]['reg_pcs'];					
					$_data['reg_set'] 				= $detail[$i]['reg_set'];
					$_data['free_pcs'] 				= $detail[$i]['free_pcs'];
					$_data['free_set'] 				= $detail[$i]['free_set'];
					$_data['ket'] 					= $detail[$i]['ket'];
					
					$this->Po_model->add_sttb($_data);
				}
			}
		$data['default'] = $this->Po_model->get_cetak_surat_sttb($ins);
		$data['week'] = $this->Penjualan_model->selectWeek();
		$this->load->view('admin_views/po/cetak_surat_sttb', $data);
	}
	
	function get_barang_by_srb($srb){
		
		$this->db->flush_cache();
		$this->db->from('retur');
		$this->db->where('intid_retur', $srb);
		$que1 = $this->db->get();
		
		if($que1->num_rows() > 0){
			$intid_retur = $que1->row()->intid_retur;
			
			$this->db->flush_cache();
			$this->db->select('*, barang.strnama');
			$this->db->from('retur');
			$this->db->join('retur_detail', 'retur_detail.intid_retur = retur.intid_retur');
			$this->db->join('barang', 'barang.intid_barang = retur_detail.intid_barang');
			$this->db->where('retur.intid_retur', $intid_retur);
			$que2 = $this->db->get();
			
			if($que2->num_rows() > 0){
				$i=0;
				foreach($que2->result() as $row){
					echo
							'<tr>
								<td>
									<input name="detail['.$i.'][intid_detail_retur]" value="'.$row->intid_detail_retur.'" type="hidden" />
									<input name="detail['.$i.'][intid_barang]" value="'.$row->intid_barang.'" type="checkbox" />
								</td>
								<td>'.$row->strnama.'</td>
								<td><input name="detail['.$i.'][reg_pcs]" value="'.$row->reg_pcs.'" type="hidden" />'.$row->reg_pcs.'</td>
								<td><input name="detail['.$i.'][reg_set]" value="'.$row->reg_set.'" type="hidden" />'.$row->reg_set.'</td>
								<td><input name="detail['.$i.'][free_pcs]" value="'.$row->free_pcs.'" type="hidden" />'.$row->free_pcs.'</td>
								<td><input name="detail['.$i.'][free_set]" value="'.$row->free_set.'" type="hidden" />'.$row->free_set.'</td>
								<td><input name="detail['.$i.'][ket]" value="'.$row->ket.'" type="hidden" />'.$row->ket.'</td>
							</tr>'
					;
					
					$i++;
				}
			}
		}
	}
	
	function sttbdata(){
	
		$week = $this->Po_model->selectWeek();
		foreach ($week as $g)
		{
			$data['id'][]	 	= $g->id;
			$data['intid_week'][] 	= $g->intid_week;
		}
		
		if($_POST) {
                        
            $id_cabang = $this->input->post('id_cabang');
			$week = $this->input->post('week');
			$data['default'] = $this->Po_model->get_data_sttb($id_cabang, $week);
            $this->load->view('admin_views/po/sttbdata', $data);
        }else {
            
            $this->load->view('admin_views/po/sttbdata', $data);
        }
		
	}
	
	function view_sttb($id) {
		
		$data['default'] = $this->Po_model->get_cetak_surat_sttb($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
		$this->load->view('admin_views/po/cetak_surat_sttb', $data);
    }
	
	function view_kartustok() {
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('po/view_kartustok/');
		$config['total_rows'] = $this->Po_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		
		$id_cabang = $this->input->post('id_cabang');
		$tgl = $this->input->post('datetgl');
		$id_barang = $this->input->post('id_barang');
		$data['po']	= $this->Po_model->getKartu_Stok($this->limit, $offset, $id_cabang, $tgl, $id_barang);
        $this->load->view('admin_views/po/view_kartustok', $data);
    }
	
	function preview_opname(){
		
		$pilih = $this->input->post('pilih');
		$nama = $this->input->post('nama');
		$pcsakhir = $this->input->post('pcsakhir');
		$setakhir = $this->input->post('setakhir');
		
		$fisikpcs = $this->input->post('fisikpcs');
		$fisikset = $this->input->post('fisikset');
		
		$selisihpcs = $this->input->post('selisihpcs');
		$selisihset = $this->input->post('selisihset');
		 		
		
		for($a=1;$a<=count($pilih);$a++){
			if(!empty($pilih[$a])){
				$data['op'][] = array(
				'nama' => $nama[$a],
				'pcsakhir' => $pcsakhir[$a],
				'setakhir' => $setakhir[$a],
				'fisikpcs' => $fisikpcs[$a],
				'fisikset' => $fisikset[$a],
				'selisihpcs' => $selisihpcs[$a],
				'selisihset' => $selisihset[$a]
				
				);
				$this->Po_model->endStok($pilih[$a], $selisihpcs[$a], $selisihset[$a], $this->input->post('intid_cabang'));
			}
		}
		
		$this->load->view('admin_views/po/preview_opname', $data);
	}
	
	function rekapkeluarbarang() {
		
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
		$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		$week = $this->Laporan_model->selectWeek();
		foreach ($week as $g)
		{
			$data['id'][]	 		= $g->id;
			$data['intid_week'][] 	= $g->intid_week;
		}
		$this->load->view('admin_views/po/param_keluarbrg', $data);
    }
	
	function cetak_brgmingguan()
	{
		$data['week']= $this->input->post('intid_week');
		$nm_cabang = $this->Cabang_model->select($this->input->post('intid_cabang'));
        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
		$data['default'] = $this->Po_model->get_CetakPengeluaranBarangMingguan($this->input->post('intid_week'), $this->input->post('intid_cabang'));
        $this->load->view('admin_views/po/keluar_barang_mingguan', $data);
	}
	
	function inputstokpusat(){
	
		if($_POST) {
            $detail =  $this->input->post('pilih');
			for($i=0; $i<count($detail); $i++)
			{
				$this->Po_model->inputStok($detail[$i]);
			}
					
			
			//redirect('po');
        }else {
            
            $data['barang'] = $this->Po_model->getBarang();
			$this->load->view('admin_views/po/input_stok', $data);
        }
		
	}
}
?>
